package com.feinno.urcs.group.log;

import com.feinno.database.Database;
import org.helium.framework.annotations.FieldSetter;
import org.helium.framework.annotations.TaskImplementation;
import org.helium.framework.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;

/**
 * Created by honghao on 10/23/15.
 */
@TaskImplementation(event = MessageLogArgs.TASK_EVENT_NAME)
public class GroupMessageLogTask implements Task<MessageLogArgs> {
	@FieldSetter("${LOGDB}")
	private Database db;

	public static String insertSql = "insert into log_groupmessage(messageid,owneruri,ownersite,peeruri,peersite,peerurilist,peerurilistcount,referby,useragent,clientip,accessaddress,messagedirection,messagetype,messageflags,messagekindtype,contenttype,content,filesize,filename,statuscode,reason,messagetime,messageflux,messageextend)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

	private static final Logger LOGGER = LoggerFactory.getLogger(GroupMessageLogTask.class);

	@Override
	public void processTask(MessageLogArgs messageLogArgs) {
		LOGGER.info("GroupMessageLogTask Start: {}", messageLogArgs);

		try {
			db.executeNonQuery(insertSql, messageLogArgs.getMessageId(), messageLogArgs.getOwnerUri(), messageLogArgs.getOwnerSite(),
					messageLogArgs.getPeerUri(), messageLogArgs.getPeerSite(), messageLogArgs.getPeerUriList(), messageLogArgs.getPeerUriListCount(), messageLogArgs.getReferBy(),
					messageLogArgs.getUserAgent(), messageLogArgs.getClientIp(), messageLogArgs.getAccessAddress(), messageLogArgs.getMessageDirection().intValue(),
					messageLogArgs.getMessageType().intValue(), messageLogArgs.getMessageFlags(), messageLogArgs.getMessageKindType().intValue(),
					messageLogArgs.getContentType().intValue(), messageLogArgs.getContent(), messageLogArgs.getFileSize(), messageLogArgs.getFileName(),
					messageLogArgs.getStatusCode(), messageLogArgs.getReason(), messageLogArgs.getMessageTime(), messageLogArgs.getMessageFlux(),
					messageLogArgs.getMessageExtend());
			LOGGER.info("GroupMessageLogTask Success.");
		} catch (SQLException e) {
			LOGGER.error("GroupMessageLogTask  {} Error:", messageLogArgs, e);
		}
		LOGGER.info("GroupMessageLogTask End.");
	}
}
